package com.wdress.filter;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;

public class CommonFilter implements Filter {
	private FilterConfig config = null;

	public void init(FilterConfig config)throws ServletException{
              this.config=config;
       }

	public void destroy() {
		config = null;
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)request;
		HttpSession session=req.getSession();
		String urls=req.getRequestURI();
		String hostName = request.getServerName();
		String lang="en";
		if("fr.superbdress.com".equals(hostName)){
		    session.setAttribute(Globals.LOCALE_KEY,Locale.FRENCH);
		    lang="fr";
		}else if("es.superbdress.com".equals(hostName)){
		    session.setAttribute(Globals.LOCALE_KEY,new Locale("es", "ES"));
		    lang="es";
		}else if("ru.superbdress.com".equals(hostName)){
		    session.setAttribute(Globals.LOCALE_KEY,new Locale("ru", "RU"));
		    lang="ru";
		}else if("pt.superbdress.com".equals(hostName)){
		    session.setAttribute(Globals.LOCALE_KEY,new Locale("pt", "pt"));
		    lang="pt";
		}else{
		    session.setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH);
		    lang="en";
		}
		session.setAttribute("lang",lang);
		chain.doFilter(request, response);
	}
}
